let app = getApp();

Page({
  data: {
    fidc:"",

    xiangInput:"",
    guigeInput:"",
    tijiInput:"",
    zhongInput:"",

    names: ['20*30*40', '30*40*50',  '47*34*80', '36*67*66','20*30*40', '30*40*50'],
    isHidden: true, //是否显示弹出框
    sousuoyvalue:"",   //在搜索框里输入的内容
    isSearchListShow: true,   //显示接口数据
    matchedNameList: [],   //通过filter方法对this.data.names数组进行过滤，筛选出与用户输入的关键字匹配的项，并将结果存储在matchedNameList中。
    selectedName:""    //点击后的赋值
  },
  onLoad(options) {
    var that = this
    // 接收purchaseStockin传过来的数据
    if (options.item) {
      that.setData({
        item: JSON.parse(options.item),
        fidc: JSON.parse(options.item)['fid']
      })

      console.log("接收purchaseStockin传过来的数据",that.data.item);
      console.log("接收purchaseStockin传过来的数据",that.data.fidc);
    }

    that.getPurchaseInquire()
  },

    // 选择仓库有关的
    dianji(e){
      console.log("点击了");
      const namesArray = this.data.names;
      this.setData({
        isHidden: !this.data.isHidden,
        showMask: true,
        matchedNameList: namesArray // 将型号names列表数据赋值给matchedNameList
      })
    },
  
    bindNameChange(e) {
      var that = this
      console.log("点击下拉内容事件触发了");
      let name = e.currentTarget.dataset.item;  // 通过e.currentTarget.dataset.item获取了用户点击的下拉列表中的项的数据，
      console.log("点击的下拉列表中的项的数据nnnnnnnnnnnnnnn",name);
    
      var selectedName = name
        // 更新 data 中的 selectedName
        that.setData({
          selectedName: selectedName,
          isHidden: !this.data.isHidden,
          showMask: false
        });
        console.log("点击的下拉列表中的项的数据赋值datannnnnnnnnnnnnnn", selectedName);
    },
  
    bindNameInput2(e) {
      var that = this
      console.log("获取内容事件触发了",e);
      const keyword = e.detail.value.toLowerCase();   //通过e.detail.value获取用户在输入框中输入的值，并将其转换为小写字母格式（toLowerCase()）。
      console.log("keyword我在搜索框输入的仓库id",keyword);
  
      const reg = new RegExp(keyword);   // 创建一个正则表达式reg，用于匹配用户输入的关键字。 筛选匹配项时，使用的是keyword变量
      console.log("reg",reg);
      const matchedNameList = this.data.names.filter(item => reg.test(item[0].toLowerCase()));  //通过filter方法对this.data.names数组进行过滤，筛选出与用户输入的关键字匹配的项，并将结果存储在matchedNameList中。
  
      this.setData({ matchedNameList });     //使用setData方法将matchedNameList更新到页面数据中，以便在界面上展示匹配的结果，并打印出匹配的结果。
      console.log("matchedNameList",matchedNameList);
  
      var that = this
      that.setData({
        sousuoyvalue: keyword
      })
    },

getPurchaseInquire() {
  var that = this;
  var fidpd = that.data.fidc
  console.log("订单明细数据fid",fidpd);

  var data = JSON.stringify(
    {
      "xx": {
        "formId": "ZYKJ_ZXD",
        "fields": [
          "FID","F_ZYKJ_ZXD_Entry_FEntryID","F_ZYKJ_Base_3iy.fnumber","F_ZYKJ_BaseProperty_43c","F_ZYKJ_BaseProperty_ne1","F_ZYKJ_BaseQty_pzg"
        ],
        "FilterString": "FID='"+ fidpd +"'",
        "orderby": "",
        "topRowCount": 0,
        "limit": 0,
        "StartRow": 0,
        "fields_name": [
          "FID","行id","物料编码","物料名称","规格型号","基本单位数量"
        ],
        "acctID": "66249c7f42e1e4"
      },
      "login": {
        "username": "test",
        "password": "zz333333"
      }
    }
  )

  // return
  dd.httpRequest({
    url: 'http://cloud.reece-sewing.com:9351/king_query',
    method: 'POST',
    headers: {
      "Content-Type": "application/json"
    },
    data: data,
    dataType: 'json',
    success: function (res) {
      if (res.data.code == 200) {
        console.log("装箱单200",res.data);
        var orderMaterialCodeList = res.data.data;

        that.setData({
          wuliaoList: orderMaterialCodeList,
        });

        console.log("jieguoooooooooooooo",that.data.wuliaoList);

        // 在订单明细获取成功后再获取分解明细数据
        // that.getPurchaseFenjie();

      } else {
        dd.showToast({
          type: 'fail',
          content: res.data.data,
          duration: 2000,
        });
      }
    },
    fail: function (res) {
      console.log("订单明细调用失败",res);
    },
    complete: function (res) {
    }
  });
},

  // 输入编码事件处理函数
  onBianmaInput(e){
    console.log("eeeeeeeeee",e);
    const index = e.currentTarget.dataset.index; // 获取物料索引
    const value = e.detail.value; // 获取输入框的新值
    const wuliaoList = this.data.wuliaoList.slice(); // 复制数组以避免直接修改原始数据
    wuliaoList[index]['物料编码'] = value; // 更新物料编码
    this.setData({
      wuliaoList: wuliaoList // 更新数据
    });

    console.log("更新了编码的数据",wuliaoList);
  },

  // 输入数量事件处理函数
  onNumberInput(e){
    console.log("eeeeeeeeee",e);
    const index = e.currentTarget.dataset.index; // 获取物料索引
    const value = e.detail.value; // 获取输入框的新值
    const wuliaoList = this.data.wuliaoList.slice(); // 复制数组以避免直接修改原始数据
    wuliaoList[index]['基本单位数量'] = value; // 更新数量
    this.setData({
      wuliaoList: wuliaoList // 更新数据
    });
    console.log("更新了数量的数据",wuliaoList);
  },


  onXiangInput(e){
    var that = this
    var xiangInput = e.detail.value
    that.setData({
      xiangInput: xiangInput,
    });
    console.log("这是输入的箱号",that.data.xiangInput);
  },
  
  onGuigeInput(e){
    var that = this
    var guigeInput = e.detail.value
    that.setData({
      guigeInput: guigeInput,
    });
    console.log("这是输入的规格",that.data.guigeInput);
  },
  
  onTijiInput(e){
    var that = this
    var tijiInput = e.detail.value
    that.setData({
      tijiInput: tijiInput,
    });
    console.log("这是输入的体积",that.data.tijiInput);
  },
  
  onZhongInput(e){
    var that = this
    var zhongInput = e.detail.value
    that.setData({
      zhongInput: zhongInput,
    });
    console.log("这是输入的重量",that.data.zhongInput);
  },
  


  // 确定修改 添加单据体
  qurrenBtny(e) {
    var that = this
    var selectedData = that.data.wuliaoList; 
    console.log("确定修改添加单据体",selectedData);

      // 3.通过循环将拿到的数据转换为接口所需要的数据结构
      var F_ZYKJ_CGRK_Entry = []
      for(var i =0; i<selectedData.length; i++){
        F_ZYKJ_CGRK_Entry.push({
          "FEntryID": selectedData[i].行id,                                                     //行id
          "F_ZYKJ_Base_3iy":  {"FNUMBER": selectedData[i].物料编码},                            //单据编码
          "F_ZYKJ_BaseQty_pzg": selectedData[i].基本单位数量,    //基本单位数量
          "F_ZYKJ_Base_qtr": {"FNUMBER": app.globaldata.user['员工工号']},                            //拣货员
        })
  
      }
      console.log("点击提交按钮修改后的item为true的接口所需要的数据结构",F_ZYKJ_CGRK_Entry);

    var fidpd = that.data.fidc
    console.log("订单明细数据确定修改fid",fidpd);

    var xiangInput = that.data.xiangInput  //箱号
    var guigeInput = that.data.selectedName  //下拉选中的规格
    var tijiInput = that.data.tijiInput   //体积
    var zhongInput = that.data.zhongInput //重量
    
    var data = JSON.stringify(
      {
        "save": {
          "formid": "ZYKJ_ZXD",
          "model": {"FID": fidpd,"F_ZYKJ_Text_ca9": xiangInput,"F_ZYKJ_Qty_dvn": tijiInput,"F_ZYKJ_Qty_w5c": zhongInput,"F_ZYKJ_Combo_yrr": guigeInput,"F_ZYKJ_ZXD_Entry":F_ZYKJ_CGRK_Entry},
          "IsAutoSubmitAndAudit": false,
          "IsDeleteEntry": false
        },
        "login": {
          "acctID": "66249c7f42e1e4",
          "url": "192.168.17.253",
          "username": "test",
          "password": "zz333333"
        }
      }
    )

    // dd.confirm({
    //   title: '温馨提示',
    //   content: data,
    //   confirmButtonText: '确定',
    //   cancelButtonText: 'no',
    // });


    my.showLoading({
      content: '',
    })
    console.log("点击提交创建的data",data);
  
    dd.httpRequest({
      url: 'http://cloud.reece-sewing.com:9351/king_save',
      method: 'POST',
      headers: {
        "Content-Type": "application/json"
      },
      data: data,
      dataType: 'json',
      success: function (res) {
        if(res.data.code == 200){
        console.log("确定修改 添加单据体200======》",res);
        dd.confirm({
          title: '温馨提示',
          content: '提交成功',
          confirmButtonText: '确认',
          cancelButtonText: '',
          success: (result) => {
            console.log(result);
            let pages = getCurrentPages();
            if (result.confirm) {
              if (pages.length >= 2) {
                dd.navigateBack({
                  delta: 1 // 返回的层级，这里表示返回两层
                });
                my.hideLoading(); // 隐藏 loading 提示框
              }
            }else {

              let pages = getCurrentPages();
              if (pages.length >= 2) {
                dd.navigateBack({
                  delta: 1 // 返回的层级，这里表示返回两层
                });
                my.hideLoading(); // 隐藏 loading 提示框
              }
            }
      }
    });

        }else{
          my.hideLoading(); // 隐藏 loading 提示框
          dd.alert({ content: errorMessage });
          console.log("采购入库单修改提交按钮的code!=200响应",res);
        }
      
      },
      fail: function (res) {
        console.log("提交按钮的fail响应",res);
        my.hideLoading();
      },
      complete: function (res) {
        my.hideLoading();
      }
    });
  },


});
